<?

$catIds = $this->mapper->GetSubElementsIds($this->cid);
$details = $this->mapper->GetById($this->cid);

if (!$details)
{
?>	
	Нет разделов
<?php
	return;
}

$rootElem = $this->mapper->GetByUrl(''); 
$rootId = $rootElem ? $rootElem->GetId() : 0; 

$numRows = sizeof($catIds);
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0"
	class="edit_block">
	<tr>
		<td class="first cur"><?=$details->GetName();?></td>
<?
if ($this->cid) 
{
    ?>
		<td width="1"><a
			href="<?=MozgRoute::Get('http')->Create(array('module' => 'admin', 'ctrl' => 'structure', 'action' => 'add_cat', 'cat' => $this->cid , 'subcat' => $this->cid));?>"
			class="edit"><img
			src="<?=_ROOT_URL;?>files/images/admin/edit_edit.gif" alt="edit"
			title="" width="30" height="11"></a></td>
		<td width="1"><a
			href="<?=MozgRoute::Get('http')->Create(array('module' => 'admin', 'ctrl' => 'structure', 'action' => 'edit_cat_body', 'cat' => $this->cid , 'subcat' => $this->cid));?>"
			class="html"><img
			src="<?=_ROOT_URL;?>files/images/admin/edit_html.gif" alt="html"
			title="" width="23" height="11"></a></td>
		<td width="1" class="last">
<?
    if (($this->cid != $rootId && !$details->GetBlocked()) || $this->isSuperUser) {
        ?>
			<a
			href="<?=MozgRoute::Get('http')->Create(array('module' => 'admin', 'ctrl' => 'structure', 'action' => 'del_cat', 'cat' => $this->cid , 'subcat' => $this->cid));?>"
			class="del" title="Удалить раздел?"><img
			src="<?=_ROOT_URL;?>files/images/admin/edit_close.gif" alt="close"
			title="" width="9" height="11"></a>
<?
    }
    ?>
		</td>
<?
}
?>
	</tr>
</table>

<?
if ($numRows) {
    ?>
<div id="catContainer" style="padding: 1px 0 1px 0;">
<div class="cat_style_hidden"></div>
<table width="100%" border="0" cellspacing="0" cellpadding="0"
	class="edit_block" id="categoriesList">
<?
    $i = 1;
    while (list (, $id) = each($catIds)) 
    {
    	$val = $this->mapper->GetById($id);
?>
<tr id="scat_<?=$val->GetId();?>">
		<td class="title"><a
			href="<?=MozgRoute::Get('http')->Create(array('module' => 'admin', 'ctrl' => 'structure', 'action' =>  'out_subcategories', 'cat' => $val->GetId()));?>"
			class="title"><?=$val->GetName();?></a></td>
		<td width="1"><a
			href="<?=MozgRoute::Get('http')->Create(array('module' => 'admin', 'ctrl' => 'structure', 'action' =>  'add_cat', 'cat' => $this->cid , 'subcat' => $val->GetId()));?>"><img
			src="<?=_ROOT_URL;?>files/images/admin/edit_edit.gif" alt="edit"
			title="" width="30" height="11"></a></td>
		<td width="1"><a
			href="<?=MozgRoute::Get('http')->Create(array('module' => 'admin', 'ctrl' => 'structure', 'action' =>  'edit_cat_body', 'cat' => $this->cid , 'subcat' => $val->GetId()));?>"><img
			src="<?=_ROOT_URL;?>files/images/admin/edit_html.gif" alt="html"
			title="" width="23" height="11"></a></td>
		<td width="1" class="last">
<?
        if (!$val->GetBlocked() || $this->isSuperUser) {
            ?>
		<a
			href="<?=MozgRoute::Get('http')->Create(array('module' => 'admin', 'ctrl' => 'structure', 'action' => 'del_cat', 'cat' => $this->cid , 'subcat' => $val->GetId()));?>"
			title="Удалить раздел?" class="del cat_del"><img
			src="<?=_ROOT_URL;?>files/images/admin/edit_close.gif" alt="close"
			title="" width="9" height="11"></a>
<?
        }
        ?>
	</td>
	</tr>
<?
        $i ++;
    }
    ;
    ?>
	</table>
</div>
<?
}
?>
<div class="bt_area"><input type="button" class="bt" value="Добавить"
	onclick="window.location.href='<?=MozgRoute::Get('http')->Create(array('module' => 'admin', 'ctrl' => 'structure', 'action' => 'add_cat', 'cat' => $this->cid));?>';" />
</div>
<script type="text/javascript">
var catList;

$(document).ready(
  function()
  {
  //----- sort ---------------------
	catList = $("#categoriesList");

	var sort_hlpr = $('#catContainer .cat_style_hidden');

	catList.sortable({
		isUpdated: false,
		axis: "y",
		scroll: false,
		items: "tr",
		containment: $('#RightPane'),
		helper: function (ev, el){
					this.helperObj = sort_hlpr.clone();
					return this.helperObj.html(el.children('.title').html()).appendTo('#catContainer');
		},		
		update: function () { this.isUpdated = true; },
		stop: function () {
				this.helperObj.remove();
				if (this.isUpdated) {
					if (confirm('Сохранить новую сортировку?')) UpdateCatSort();
					this.isUpdated = false;
				}
		}
	});
	//-----------------------------

	//------- delete --------------

	//-----------------------------
  }
);

function UpdateCatSort()
{
	$.ajax( {url: "<?=MozgRoute::Get('http')->Create(array('module' => 'admin', 'ctrl' => 'structure', 'action' => 'update_sort', 'cat' => $this->cid));?>",
			dataType: 'json',
			type: 'POST',
			data: catList.sortable("serialize"),
			success:function (data){
						if (data.status == 0)
						{
							window.location.href = window.location.href;
							return true;
						}

						showMessage(data.message, true);
						return false;
					}
	} );
	return false;
}
</script>